<?php

/**
 * PersonalMemberData form base class.
 *
 * @method PersonalMemberData getObject() Returns the current form's model object
 *
 * @package    Wildlive
 * @subpackage form
 * @author     Maximilian Lenkeit <dev@lenki.com>
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BasePersonalMemberDataForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                         => new sfWidgetFormInputHidden(),
      'member_id'                  => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Member'), 'add_empty' => true)),
      'bank_account_id'            => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('BankAccount'), 'add_empty' => true)),
      'email_address'              => new sfWidgetFormInputText(),
      'date_of_birth'              => new sfWidgetFormDate(),
      'birthplace'                 => new sfWidgetFormInputText(),
      'direct_debit_authorization' => new sfWidgetFormInputCheckbox(),
      'created_at'                 => new sfWidgetFormInputText(),
      'updated_at'                 => new sfWidgetFormInputText(),
      'deleted_at'                 => new sfWidgetFormInputText(),
      'created_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'add_empty' => true)),
      'updated_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Updator'), 'add_empty' => true)),
      'deleted_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Deletor'), 'add_empty' => true)),
      'version'                    => new sfWidgetFormInputText(),
      'addresses_list'             => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Address')),
      'phone_numbers_list'         => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'PhoneNumber')),
    ));

    $this->setValidators(array(
      'id'                         => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'member_id'                  => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Member'), 'required' => false)),
      'bank_account_id'            => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('BankAccount'), 'required' => false)),
      'email_address'              => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'date_of_birth'              => new sfValidatorDate(array('required' => false)),
      'birthplace'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'direct_debit_authorization' => new sfValidatorBoolean(array('required' => false)),
      'created_at'                 => new sfValidatorPass(array('required' => false)),
      'updated_at'                 => new sfValidatorPass(array('required' => false)),
      'deleted_at'                 => new sfValidatorPass(array('required' => false)),
      'created_by'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'required' => false)),
      'updated_by'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Updator'), 'required' => false)),
      'deleted_by'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Deletor'), 'required' => false)),
      'version'                    => new sfValidatorInteger(array('required' => false)),
      'addresses_list'             => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Address', 'required' => false)),
      'phone_numbers_list'         => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'PhoneNumber', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorDoctrineUnique(array('model' => 'PersonalMemberData', 'column' => array('id'))),
        new sfValidatorDoctrineUnique(array('model' => 'PersonalMemberData', 'column' => array('member_id'))),
        new sfValidatorDoctrineUnique(array('model' => 'PersonalMemberData', 'column' => array('bank_account_id'))),
      ))
    );

    $this->widgetSchema->setNameFormat('personal_member_data[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'PersonalMemberData';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['addresses_list']))
    {
      $this->setDefault('addresses_list', $this->object->Addresses->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['phone_numbers_list']))
    {
      $this->setDefault('phone_numbers_list', $this->object->PhoneNumbers->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveAddressesList($con);
    $this->savePhoneNumbersList($con);

    parent::doSave($con);
  }

  public function saveAddressesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['addresses_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Addresses->getPrimaryKeys();
    $values = $this->getValue('addresses_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Addresses', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Addresses', array_values($link));
    }
  }

  public function savePhoneNumbersList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['phone_numbers_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->PhoneNumbers->getPrimaryKeys();
    $values = $this->getValue('phone_numbers_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('PhoneNumbers', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('PhoneNumbers', array_values($link));
    }
  }

}
